<?php
	class Timu extends MY_controller{

		function __construct(){
			parent::__construct();
			$letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
			$this->smarty->assign("letter", $letter);
		}

		function index($subject_id = 0, $book_id = 0, $title = 0,$limit = 0){
			$this->load->model("Timus");
			$post_data = $this->input->post();
			if(!empty($post_data['subject_id'])){
				$subject_id = $post_data['subject_id'];
			}

			if(!empty($post_data['book_id'])){
				$book_id = $post_data['book_id'];
			}

			if(!empty($post_data['title'])){
				$title = $post_data['title'];
			}

			$conditions['subject_id'] = $subject_id;
			$conditions['book_id'] = $book_id;
			$conditions['title'] = $title;
			$result = $this->Timus->get_timu_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/timu/index/'.$subject_id.'/'.$book_id.'/'.$title;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 6;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("timu_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("action", '题目列表');
			$this->smarty->display("timu/list.html");
		}

		function show_children($timu_id = 0, $type = 0, $title = 0, $limit = 0){
			if(empty($timu_id)){
				show_message("/timu/save_main", "缺少题目ID");exit;
			}
			$this->load->model("Timus");
			$post_data = $this->input->post();
			
			if(!empty($post_data['type'])){
				$type = $post_data['type'];
			}
			if(!empty($post_data['title'])){
				$title = $post_data['title'];
			}
			$conditions['type'] = $type;
			$conditions['title'] = $title;
			$conditions['timu_id'] = $timu_id;

			$result = $this->Timus->get_children_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/timu/show_children/';
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("timu_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("action", '题目列表');
			$this->smarty->display("timu/show_children.html");
		}

		//添加
		function save_main($timu_id = 0){
			if($_POST){
				$post_data = $this->input->post();
				if($timu_id > 0){
					$post_data['timu_main_id'] = $timu_id;
				}
				if(empty($post_data['type'])){
					show_message("/timu/save_main/".$timu_id, "缺少题目类型");exit;
				}
				switch ($post_data['type']) {
					case 1:
						$timu_main_id = $this->save_danxuan($post_data);
					break;
					case 2:
						$timu_main_id = $this->save_duoxuan($post_data);
					break;
					case 3:
						$timu_main_id = $this->save_panduan($post_data);
					break;
					case 4:
						$timu_main_id = $this->save_jianda($post_data);
					break;
					case 5:
						$timu_main_id = $this->save_tiankong($post_data);
					break;
					case 6:
					break;
					case 7:
						$timu_main_id = $this->save_suanshi($post_data);
					break;
				}
				if(isset($post_data['continue'])){
					header("Location:/timu/save_main/".$timu_main_id);exit;
				}else{
					header("Location:/timu");exit;
				}
			}
			if($timu_id > 0){
				$timu_info = get_field_by_where("tk_timu_main", "*", "id='{$timu_id}'");
				// print_r($timu_info);
				$this->smarty->assign("timu_info", $timu_info);
			}
			$this->load->model('Subject_model');
			$subject = $this->Subject_model->get_subject_all();
			$this->smarty->assign("subject", $subject);
			$this->smarty->display("timu/save_main.html");
		}

		//添加单选题
		function save_danxuan($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['danxuan'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['danxuan']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}
			if(empty($data['danxuan']['radio_text'])){
				show_message("/timu/save_main", "缺少子题目答案");exit;
			}
			if(empty($data['danxuan']['an_radio'])){
				show_message("/timu/save_main", "没有选择正确答案");exit;
			}
			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}
			$this->db->trans_start();
			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}

			//写入子题目
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $data['danxuan']['title'];
			$timu_children['analysis '] = !empty($data['danxuan']['analysis']) ? $data['danxuan']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id

			//写入子题目答案
			$timu_children_result = array();
			foreach ($data['danxuan']['radio_text'] as $key => $value) {
				if($key == $data['danxuan']['an_radio']){
					$is_right = 1;
				}else{
					$is_right = 0;
				}
				$result_data['timu_id'] 			= $timu_main_id;
				$result_data['timu_children_id'] 	= $timu_children_id;
				$result_data['answer_text'] 		= $value;
				$result_data['is_right'] 			= $is_right;
				$result_data['type'] 				= 1;
				$timu_children_result[] 			= $result_data;
			}
			$this->db->insert_batch("tk_timu_children_result", $timu_children_result);
			$this->db->trans_complete();
			return $timu_main_id;
		}

		function save_duoxuan($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['duoxuan'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['duoxuan']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}
			if(empty($data['duoxuan']['checkbox_text'])){
				show_message("/timu/save_main", "缺少子题目答案");exit;
			}
			if(empty($data['duoxuan']['checkbox'])){
				show_message("/timu/save_main", "没有选择正确答案");exit;
			}
			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}
			$this->db->trans_start();

			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}

			//写入子题目
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $data['duoxuan']['title'];
			$timu_children['analysis '] = !empty($data['duoxuan']['analysis']) ? $data['duoxuan']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id

			//写入子题目答案
			$timu_children_result = array();
			foreach ($data['duoxuan']['checkbox_text'] as $key => $value) {
				if(!empty($data['duoxuan']['checkbox'][$key])){
					$is_right = 1;
				}else{
					$is_right = 0;
				}
				$result_data['timu_id'] 			= $timu_main_id;
				$result_data['timu_children_id'] 	= $timu_children_id;
				$result_data['answer_text'] 		= $value;
				$result_data['is_right'] 			= $is_right;
				$result_data['type'] 				= 1;
				$timu_children_result[] 			= $result_data;
			}
			$this->db->insert_batch("tk_timu_children_result", $timu_children_result);
			$this->db->trans_complete();
			return $timu_main_id;
		}

		function save_panduan($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['panduan'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['panduan']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}
			if(empty($data['panduan']['choice_text'])){
				show_message("/timu/save_main", "缺少子题目答案");exit;
			}
			if(empty($data['panduan']['choice'])){
				show_message("/timu/save_main", "没有选择正确答案");exit;
			}
			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}
			$this->db->trans_start();

			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}

			//写入子题目
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $data['panduan']['title'];
			$timu_children['analysis '] = !empty($data['panduan']['analysis']) ? $data['panduan']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id

			//写入子题目答案
			$timu_children_result = array();
			foreach ($data['panduan']['choice_text'] as $key => $value) {
				if($key == $data['panduan']['choice']){
					$is_right = 1;
				}else{
					$is_right = 0;
				}
				$result_data['timu_id'] 			= $timu_main_id;
				$result_data['timu_children_id'] 	= $timu_children_id;
				$result_data['answer_text'] 		= $value;
				$result_data['is_right'] 			= $is_right;
				$result_data['type'] 				= 1;
				$timu_children_result[] 			= $result_data;
			}
			$this->db->insert_batch("tk_timu_children_result", $timu_children_result);
			$this->db->trans_complete();
			return $timu_main_id;
		}

		function save_jianda($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['jianda'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['jianda']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}

			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}
			$this->db->trans_start();

			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}
			
			//写入子题目
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $data['jianda']['title'];
			$timu_children['analysis '] = !empty($data['jianda']['analysis']) ? $data['jianda']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id

			$this->db->trans_complete();
			return $timu_main_id;
		}

		function save_tiankong($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['tiakong'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['tiakong']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}

			if(empty($data['tiakong']['text']) && empty($data['tiakong']['symbol']) && empty($data['tiakong']['number']) && empty($data['tiakong']['calc'])){
				show_message("/timu/save_main", "缺少子题目答案");exit;
			}

			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}

			$this->db->trans_start();

			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}

			//写入子题目
			$title = $data['tiakong']['title'];
			$title = cleanhtml($title);//过滤img以外的html标签
			$title = str_replace('&nbsp;','',$title);
			$regex1 = '/（[?=\s]+）/';
			$title = preg_replace($regex1, "____", $title); //把括号替换成下划线
			//重新赋值
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $title;
			$timu_children['analysis '] = !empty($data['tiakong']['analysis']) ? $data['tiakong']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id
			//写入子题目答案
			$timu_children_result = array();
			foreach ($data['tiakong'] as $key => $value) {
				$type = 0;
				if($key == 'text'){
					$type = 1;
				}elseif($key == 'symbol'){
					$type = 2;
				}elseif($key == 'number'){
					$type = 3;
				}elseif($key == 'calc'){
					$type = 4;
				}
				if($type > 0){
					$result_data['timu_id'] 			= $timu_main_id;
					$result_data['timu_children_id'] 	= $timu_children_id;
					$result_data['answer_text'] 		= current($value);
					$result_data['is_right'] 			= 1;
					$result_data['type'] 				= $type;
					$timu_children_result[] 			= $result_data;
				}
			}
			$this->db->insert_batch("tk_timu_children_result", $timu_children_result);
			$this->db->trans_complete();
			return $timu_main_id;
		}


		function save_suanshi($data = array()){
			if(!$data){
				show_message("/timu", "非法操作");exit;
			}
			if(empty($data['suanshi'])){
				show_message("/timu/save_main", "缺少子题目");exit;
			}
			if(empty($data['suanshi']['title'])){
				show_message("/timu/save_main", "缺少子题目内容");exit;
			}

			if(empty($data['suanshi']['calc'])){
				show_message("/timu/save_main", "缺少子题目答案");exit;
			}

			//查询课本
			$this->load->model('Subject_book_model');
			$book_info = $this->Subject_book_model->get_book_detail($data['book_id']);
			if(!$book_info){
				show_message("/timu/save_main", "没有选择课本");exit;
			}

			$this->db->trans_start();

			if(!empty($data['timu_main_id'])){
				$timu_main_id = $data['timu_main_id'];
			}else{
				//写入主题目
				$timu_main['subject_id'] = $data['subject_id'];
				$timu_main['book_id'] = $data['book_id'];
				$timu_main['book_chapter_id'] = $data['chapter_id'];
				$timu_main['grade'] = $book_info['grade'];
				$timu_main['semester'] = $book_info['semester'];
				$timu_main['difficulty_level'] = $data['difficulty_level'];
				$timu_main['title'] = !empty($data['title']) ? $data['title'] : '';
				$timu_main['add_time'] = time();
				$timu_main['subject_id'] = $data['subject_id'];
				$this->db->insert("tk_timu_main", $timu_main);
				$timu_main_id = $this->db->insert_id();//主题目id
			}

			//写入子题目
			$timu_children['timu_id '] 	= $timu_main_id;
			$timu_children['type '] 	= $data['type'];
			$timu_children['title '] 	= $data['suanshi']['title'];
			$timu_children['analysis '] = !empty($data['suanshi']['analysis']) ? $data['suanshi']['analysis'] : '';
			$this->db->insert("tk_timu_children", $timu_children);
			$timu_children_id = $this->db->insert_id();//主题目id

			//写入子题目答案
			$calc_array = array(
								"1"=>"+",
								"2"=>"-",
								"3"=>"*",
								"4"=>"/",
								"5"=>"(",
								"6"=>")",
								"7"=>">",
								"8"=>"≥",
								"9"=>"<",
								"10"=>"≤",
								"11"=>"=",
								"12"=>"[",
								"13"=>"]"
							);
			$timu_children_result = array();
			foreach ($data['suanshi']['calc'] as $key => $val) {
				$suanshi_array = formula($val);
				foreach ($suanshi_array as $j => $v) {
					if(is_numeric($v)){
						$type = 1;
						$value = $v;
					}else{
						$type = 2;
						$value = array_search($v, $calc_array);
					}

					$timu_children_result[] = array(
												'timu_children_id' => $timu_children_id,
												'value' => $value,
												'type'  => $type,
												'group' => $key
												);
				}
			}

			$this->db->insert_batch("tk_timu_children_result_formula", $timu_children_result);
			$this->db->trans_complete();
			return $timu_main_id;
		}

		function load_danxuan(){
			$this->smarty->display("timu/save_danxuan.html");
		}

		function load_duoxuan(){
			$this->smarty->display("timu/save_duoxuan.html");
		}

		function load_panduan(){
			$this->smarty->display("timu/save_panduan.html");
		}

		function load_jianda(){
			$this->smarty->display("timu/save_jianda.html");
		}

		function load_tiankong(){
			$this->smarty->display("timu/save_tiankong.html");
		}

		function load_jisuan(){
			$this->smarty->display("timu/save_jisuan.html");
		}

		function load_suanshi(){
			$this->smarty->display("timu/save_suanshi.html");
		}

		function load_lianxian(){
			$this->smarty->display("timu/save_lianxian.html");
		}

		function load_kantixieshu(){
			$this->smarty->display("timu/save_kantixieshu.html");
		}

		function load_kantuhuaxing(){
			$this->smarty->display("timu/save_kantuhuaxing.html");
		}
	}